package cn.acheng.cams.infrastructure.persistent.repository;

import cn.acheng.cams.domain.user.model.dto.LogonDTO;
import cn.acheng.cams.domain.user.model.entity.UserEntity;
import cn.acheng.cams.domain.user.repository.IUserRepository;
import cn.acheng.cams.infrastructure.persistent.dao.UserDao;
import cn.acheng.cams.infrastructure.persistent.po.User;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Repository;

@Repository
public class UserRepository implements IUserRepository {

    @Resource
    private UserDao userDao;

    @Override
    public UserEntity selectUserByUsernameAndRole(String username, String role) {
        User user = userDao.selectUserByUsernameAndRole(username,role);
        if(user == null){
            return null;
        }
        UserEntity userEntity = new UserEntity();
        userEntity.setPasswordHash(user.getPasswordHash());
        userEntity.setRole(user.getRole());
        userEntity.setUsername(user.getUsername());
        return userEntity;
    }

    @Override
    public void insert(LogonDTO logonDTO) {
        User user = new User();
        user.setUsername(logonDTO.getUsername());
        user.setPasswordHash(logonDTO.getPassword());
        user.setRealName(logonDTO.getRealName());
        user.setRole(logonDTO.getRole());
        user.setDepartment(logonDTO.getDepartment());
        userDao.insert(user);
    }
}
